#pragma once
#include <assert.h>
#include <iostream>

namespace jw
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		iterator begin() const
		{
			return _start;
		}

		iterator end() const
		{
			return _finish;
		}

		void size() const
		{
			return _finish - _start;
		}

		void capacity() const
		{
			return _endofstorage - _start;
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		void push_back(const T& val)
		{
			size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity;
			T* tmp = new T[capacity];
			memcpy(tmp, _start, size() * sizeof(T));
			size_t oldsize = size();
			delete[] _start;
			_start = tmp;
			_finish = tmp + oldsize;
			_endofstorage = tmp + newcapacity;

			*_finish = val;
			++_finish;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

	template<class T>
	void print_vector(const vector<T>& v)
	{
		for (auto e : v)
		{
			cout << e << " ";
		}

		auto it = v.begin();
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << endl;
			++it;
		}

		for (size_t i = 0; i < v.size(); i++)
		{
			cout << v[i] << endl;
		}
	}

	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		print_vector(v);
	}
}
